<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="title"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      width="450px">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :disabled="isDisabled"
        label-width="100px"
      >
        <!-- <el-form-item :label="$t('eqpType')" prop="eqpType">
          <el-select v-model="form.eqpType" :disabled="eqptypedisable" :style="{width: '100%'}">
            <el-option
              v-for="item in types"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item :label="$t('eqpModel')" prop="eqpModel">
          <el-input v-model="form.eqpModel" :disabled="eqpmodeldisable" />
        </el-form-item> -->
        <el-form-item :label="$t('eqpfacility')" prop="facility">
          <el-input v-model="form.facility" />
        </el-form-item>
        <el-form-item :label="$t('eqpBrand')" prop="vendor">
          <el-input v-model="form.vendor" />
        </el-form-item>
      </el-form>
      <div v-if="!isDisabled" slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="closeDialog">{{
          $t("cancel")
        }}</el-button>
        <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{ $t('save') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/equipment/eqpModel'
import mySetting from '@/mixins/mySetting'
export default {
  name: 'MyDialog',
  mixins: [mySetting],
  props: {
    form: {
      type: Object,
      default: () => ({})
    },
    isDisabled: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      types: [],
      dialogFormVisible: false,
      eqptypedisable: false,
      eqpmodeldisable: false,
      rules: {
        eqpType: [
          { required: true, message: `${this.$t('pleaseSelect')}${this.$t('eqpType')}`, trigger: 'change' }
        ],
        eqpModel: [
          { required: true, message: `${this.$t('pleaseInput')}${this.$t('eqpModel')}`, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit({ id: this.form.id, ...this.form }).then(res => {
            this.doMerge(res)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then(res => {
            this.doMerge(res)
          })
        }
      })
    },
    // 打开弹窗
    openDialog(val) {
      if (val === 1) {
        this.eqptypedisable = false
        this.eqpmodeldisable = false
      } else {
        this.eqptypedisable = true
        this.eqpmodeldisable = true
      }
      crudJob.getType()
        .then((res) => {
          this.types = res.data
        })
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.$emit('closeDialog')
      this.dialogFormVisible = false
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
